﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Map.Controllers
{
    public class LoginController : Controller
    {
        #region 用户登录
        // GET: AdminLogin
        public ActionResult Index()
        {
            if (Session["AdminUser"] != null)
            {
                return RedirectToAction("Index", "Admin");
            }
            return View();
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <returns></returns>
        public JsonResult UserLogin(string userName, string password)
        {
            using (var context = new MapEntities())
            {
                var query = (from item in context.Users
                             where item.Account == userName
                             select item).FirstOrDefault();

                if (query != null)
                {
                    if (query.Password == password)
                    {
                        Session["AdminUser"] = query.NickName;
                        Session["Role"] = query.Role;
                        return Json(new { result = true });
                    }
                }

                return Json(new { result = false });

            }
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <returns></returns>
        public ActionResult LoginOut()
        {
            Session["AdminUser"] = null;
            Session.Timeout = 10;

            return RedirectToAction("Index", "Admin");
        }
        #endregion
    }
}